//获取缓存中的token
import{TOKEN_KEY } from '../constants/token-const'
const token =wx.getStorageSync(TOKEN_KEY)

const BASE_URL="http://123.207.32.32:9000" //服务端网易云
// const BASE_URL="http://localhost:3000"//下载开源网易云node，服务跑在本地
const LOGIN_BASE_URL="http://123.207.32.32:3000" //已部署好的服务器获取token

class YRrequest{
  constructor(baseURL,authHeader={}){
    this.baseURL=baseURL
    this.authHeader=authHeader 
  }
                          //接口是否需要授权
  request(url,method,params,isAuth=false,header={}){
                  // 若需要授权则头部携带token以及传入的header
    const finalHeader=isAuth? {...this.authHeader,header}:header
    return new Promise((resolve,reject)=>{
      wx.request({
        url: this.baseURL+url,
        header:finalHeader,
        method:method,
        data:params,
        success:function(res){
          resolve(res)
        },
        //简写
        fail:reject
        // fail:function(err){ 
        //   reject(err)
        // }
      })
    })
  }
  get(url,params,isAuth=false,header){
    return this.request(url,"GET",params,isAuth,header)
  }
  post(url,data,isAuth=false,header){
    return this.request(url,"POST",data,isAuth,header)
  }
}
const yrRequest=new YRrequest(BASE_URL)

const yrLoginRequest=new YRrequest(LOGIN_BASE_URL,{
  token
})  
export default yrRequest
export{
  yrLoginRequest
} 